package com.itheima.dao;

import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;


import java.util.List;
import java.util.Map;

public interface SetmealDao {
    /**
     * 模糊查询
     *
     * @param queryString
     * @return
     */
    @Select("<script> " +
            "select * from t_setmeal <where>\n" +
            "            <if test=\"value!=null and value.length > 0\">\n" +
            "                code like concat('%',#{value},'%') or name like concat('%',#{value},'%') or helpCode like concat('%',#{value},'%')\n" +
            "            </if>\n" +
            "        </where>" +
            "</script> ")
    List<Setmeal> findByCondition(String queryString);

    /**
     * 添加套餐
     *
     * @param setmeal
     * @return
     */
    @Insert("insert into t_setmeal values(null,#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer saveSetmeal(Setmeal setmeal);

    /**
     * 向中间表中 添加数据
     *
     * @param setmealId
     * @param checkgroupId
     * @return
     */

    @Insert("insert into t_setmeal_checkgroup values(#{setmealId},#{checkgroupId})")
    Integer setSetmealAndCheckGroup(@Param("setmealId") Integer setmealId, @Param("checkgroupId") Integer checkgroupId);

    /**
     * 修改套餐
     *
     * @param setmeal
     * @return
     */
    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention} where id = #{id}")
    Integer editSetmeal(Setmeal setmeal);

    /**
     * 删除中间关系
     *
     * @param setmealId
     */
    @Delete("delete from t_setmeal_checkgroup where setmeal_id = #{setmealId}")
    void deleteAssociation(@Param("setmealId") Integer setmealId);


    /**
     * 删除套餐
     *
     * @param setmealId
     * @return
     */
    @Delete("delete from t_setmeal where id = #{setmealId}")
    Integer deleteSetmeal(Integer setmealId);

    @Select("select * from t_setmeal")
    List<Setmeal> findAll();

    @Select("select * from t_setmeal where id = #{setmealId}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "checkGroups", column = "id",
                    many = @Many(
                            select = "com.itheima.dao.CheckGroupDao.findCheckGroupBySetmealId"
                    )
            )
    })
    Setmeal findById(Integer setmealId);

    @Select("select s.name name,count(*) value from t_setmeal s,t_order o where o.setmeal_id=s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();

}
